package com.common.operator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;

/**
 * @author Morgan Liao
 * 
 */
public class PropertyFileOperator {

	private String fileName;

	public PropertyFileOperator(String fileName) {
		this.fileName = fileName;
	}

	public String getValue(String key) {
		Properties pro = new Properties();
		try {
			InputStream in = new FileInputStream(fileName);
			try {
				pro.load(in);
			} finally {
				in.close();
			}

			return pro.getProperty(key);
		} catch (Exception e) {
			e.printStackTrace();

		}
		return "";
	}

	/**
	 * Used to update many properties in one time.
	 * 
	 * @param datas
	 */
	public void setValue(Map<String, String> datas) {
		Properties pro = new Properties();

		try {
			InputStream in = new FileInputStream(fileName);
			try {
				pro.load(in);
			} finally {
				in.close();
			}

			Set<Map.Entry<String, String>> entries = datas.entrySet();
			Iterator<Entry<String, String>> it = entries.iterator();
			while (it.hasNext()) {
				Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
				pro.setProperty(entry.getKey().toString(), entry.getValue()
						.toString());
			}

			OutputStream out = new FileOutputStream(fileName);
			try {
				pro.store(out, "Modify it By Morgan");
				out.flush();
			} finally {
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Used to update one pair of key and value.
	 * 
	 * @param key
	 * @param value
	 */
	public void setValue(String key, String value) {
		
        File file = new File(fileName);
        BufferedReader reader = null;
        List<String> fileContent = new ArrayList<String>();
        
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = "";
            while ((tempString = reader.readLine()) != null) {
            	String[] splits = tempString.split("=", 2);
            	
            	if(splits.length >=2 && splits[0].equals(key)) {
            		fileContent.add(key + "=" + value);
            	} else {
                    fileContent.add(tempString);
            	}
            }
            reader.close();

            
			PrintWriter outputStream = new PrintWriter(fileName);
			for (String line : fileContent) {
				outputStream.println(line);
			}
			outputStream.close();
			
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                	e1.printStackTrace();
                }
            }
            
            
        }
	}

}
